capture log close
log using "Y:\Documents\Output\analysis", replace text

*********************************************
* do-file to run conditional logit models	*
* for robustness check with additional		*
* "other" major								*
* fo 02.07.2015								*	
*********************************************

version 13
clear
set more off
cd "Y:\Documents\"
use "Data\data_choice_long.dta"

*** Globals ***
global maineffects = "fach1-fach7 fach9-fach24" 	/* refcat: econ sciences */
global modnr = 16									/* number of models to be stored in matrix */

****************************************************
***		 multivariate analysis of effects		 ***
****************************************************
clogit choice $maineffects mathintxdoer carexdoer mathintxthinker carexthinker mathintxcreator carexcreator mathintxhelper carexhelper mathintxpersuader carexpersuader mathintxorganizer carexorganizer apfmeltxfrau apfmpeerxfrau mathintxleist_md intercptxbread hrsworkxflexi sdiscrimxfrau 		[pw=w_t13_std], group(id_ind) or
	est sto a_m1
	esttab a_m1 using "Output\robustness1_directeffects.csv", replace b(a4) se eform

clogit choice $maineffects std_mathintxdoer std_carexdoer std_mathintxthinker std_carexthinker std_mathintxcreator std_carexcreator std_mathintxhelper std_carexhelper std_mathintxpersuader std_carexpersuader std_mathintxorganizer std_carexorganizer std_apfmeltxfrau std_apfmpeerxfrau std_mathintxleist_md std_intercptxbread std_hrsworkxflexi std_sdiscrimxfrau [pw=w_t13_std], group(id_ind) or
	est sto a_m2
	esttab a_m2 using "Output\robustness1_directeffects.csv", append b(a4) se eform
	
***************************************
***		 min max decomposition 		***
***************************************			

* reference models: baseline and saturated:
qui clogit choice pfemale2xfrau $maineffects 																																  																																	  		[pw=w_t13_std], group(id_ind) or
	matrix A = (e(b))
qui clogit choice pfemale2xfrau $maineffects mathintxdoer carexdoer mathintxthinker carexthinker mathintxcreator carexcreator mathintxhelper carexhelper mathintxpersuader carexpersuader mathintxorganizer carexorganizer apfmeltxfrau apfmpeerxfrau mathintxleist_md intercptxbread hrsworkxflexi sdiscrimxfrau 		[pw=w_t13_std], group(id_ind) or
	matrix B = e(b)
	
* 1 - interest: doer
qui clogit choice pfemale2xfrau $maineffects mathintxdoer carexdoer																																  																												  		[pw=w_t13_std], group(id_ind) or
	matrix E1max = e(b)	
qui clogit choice pfemale2xfrau $maineffects						mathintxthinker carexthinker mathintxcreator carexcreator mathintxhelper carexhelper mathintxpersuader carexpersuader mathintxorganizer carexorganizer apfmeltxfrau apfmpeerxfrau mathintxleist_md intercptxbread hrsworkxflexi sdiscrimxfrau 		[pw=w_t13_std], group(id_ind) or
	matrix E1min = e(b)

* 2 - interest: thinker
qui clogit choice pfemale2xfrau $maineffects  						mathintxthinker carexthinker																			  																																	 		[pw=w_t13_std], group(id_ind) or
	matrix E2max = e(b)
qui clogit choice pfemale2xfrau $maineffects mathintxdoer carexdoer 							 mathintxcreator carexcreator mathintxhelper carexhelper mathintxpersuader carexpersuader mathintxorganizer carexorganizer apfmeltxfrau apfmpeerxfrau mathintxleist_md intercptxbread hrsworkxflexi sdiscrimxfrau 		[pw=w_t13_std], group(id_ind) or
	matrix E2min = e(b)

* 3 - interest: creator
qui clogit choice pfemale2xfrau $maineffects 													 mathintxcreator carexcreator												  																																	 		[pw=w_t13_std], group(id_ind) or
	matrix E3max = e(b)
qui clogit choice pfemale2xfrau $maineffects mathintxdoer carexdoer mathintxthinker carexthinker 							  mathintxhelper carexhelper mathintxpersuader carexpersuader mathintxorganizer carexorganizer apfmeltxfrau apfmpeerxfrau mathintxleist_md intercptxbread hrsworkxflexi sdiscrimxfrau 		[pw=w_t13_std], group(id_ind) or
	matrix E3min = e(b)

* 4 - interest: helper
qui clogit choice pfemale2xfrau $maineffects 																				  mathintxhelper carexhelper						  																																	 	[pw=w_t13_std], group(id_ind) or
	matrix E4max = e(b)
qui clogit choice pfemale2xfrau $maineffects mathintxdoer carexdoer mathintxthinker carexthinker mathintxcreator carexcreator 							 mathintxpersuader carexpersuader mathintxorganizer carexorganizer apfmeltxfrau apfmpeerxfrau mathintxleist_md intercptxbread hrsworkxflexi sdiscrimxfrau 		[pw=w_t13_std], group(id_ind) or
	matrix E4min = e(b)

* 5 - interest: persuader
qui clogit choice pfemale2xfrau $maineffects 																											 mathintxpersuader carexpersuader																															 	[pw=w_t13_std], group(id_ind) or
	matrix E5max = e(b)
qui clogit choice pfemale2xfrau $maineffects mathintxdoer carexdoer mathintxthinker carexthinker mathintxcreator carexcreator mathintxhelper carexhelper								  mathintxorganizer carexorganizer apfmeltxfrau apfmpeerxfrau mathintxleist_md intercptxbread hrsworkxflexi sdiscrimxfrau 		[pw=w_t13_std], group(id_ind) or
	matrix E5min = e(b)

* 6 - interest: organizer
qui clogit choice pfemale2xfrau $maineffects 																																  			  mathintxorganizer carexorganizer																						 		[pw=w_t13_std], group(id_ind) or
	matrix E6max = e(b)
qui clogit choice pfemale2xfrau $maineffects mathintxdoer carexdoer mathintxthinker carexthinker mathintxcreator carexcreator mathintxhelper carexhelper mathintxpersuader carexpersuader 									apfmeltxfrau apfmpeerxfrau mathintxleist_md intercptxbread hrsworkxflexi sdiscrimxfrau 		[pw=w_t13_std], group(id_ind) or
	matrix E6min = e(b)
	
* 7 - essentialism: internalized total
qui clogit choice pfemale2xfrau $maineffects mathintxdoer carexdoer mathintxthinker carexthinker mathintxcreator carexcreator mathintxhelper carexhelper mathintxpersuader carexpersuader mathintxorganizer carexorganizer																							 	[pw=w_t13_std], group(id_ind) or
	matrix E7max = e(b)
qui clogit choice pfemale2xfrau $maineffects 																																												apfmeltxfrau apfmpeerxfrau mathintxleist_md intercptxbread hrsworkxflexi sdiscrimxfrau 		[pw=w_t13_std], group(id_ind) or
	matrix E7min = e(b)
	
* 8 - gender norms: parents
qui clogit choice pfemale2xfrau $maineffects 																																  												apfmeltxfrau																		 		[pw=w_t13_std], group(id_ind) or
	matrix E8max = e(b)
qui clogit choice pfemale2xfrau $maineffects mathintxdoer carexdoer mathintxthinker carexthinker mathintxcreator carexcreator mathintxhelper carexhelper mathintxpersuader carexpersuader mathintxorganizer carexorganizer  			 apfmpeerxfrau mathintxleist_md intercptxbread hrsworkxflexi sdiscrimxfrau 		[pw=w_t13_std], group(id_ind) or
	matrix E8min = e(b)	

* 9 - gender norms: peers
qui clogit choice pfemale2xfrau $maineffects 																																  															 apfmpeerxfrau 															 		[pw=w_t13_std], group(id_ind) or
	matrix E9max = e(b)
qui clogit choice pfemale2xfrau $maineffects mathintxdoer carexdoer mathintxthinker carexthinker mathintxcreator carexcreator mathintxhelper carexhelper mathintxpersuader carexpersuader mathintxorganizer carexorganizer apfmeltxfrau 				mathintxleist_md intercptxbread hrsworkxflexi sdiscrimxfrau 	[pw=w_t13_std], group(id_ind) or
	matrix E9min = e(b)	
	
* 10 - gender norms: total	
qui clogit choice pfemale2xfrau $maineffects 																																  											   apfmeltxfrau apfmpeerxfrau 																 	[pw=w_t13_std], group(id_ind) or
	matrix E10max = e(b)
qui clogit choice pfemale2xfrau $maineffects mathintxdoer carexdoer mathintxthinker carexthinker mathintxcreator carexcreator mathintxhelper carexhelper mathintxpersuader carexpersuader mathintxorganizer carexorganizer 								mathintxleist_md intercptxbread hrsworkxflexi sdiscrimxfrau 	[pw=w_t13_std], group(id_ind) or
	matrix E10min = e(b)	
	
* 11 - education: relative math grades
qui clogit choice pfemale2xfrau $maineffects 																																  																			mathintxleist_md											 	[pw=w_t13_std], group(id_ind) or
	matrix E11max = e(b) 
qui clogit choice pfemale2xfrau $maineffects mathintxdoer carexdoer mathintxthinker carexthinker mathintxcreator carexcreator mathintxhelper carexhelper mathintxpersuader carexpersuader mathintxorganizer carexorganizer apfmeltxfrau apfmpeerxfrau 				 	intercptxbread hrsworkxflexi sdiscrimxfrau 		[pw=w_t13_std], group(id_ind) or
	matrix E11min = e(b)	
	
* 12 - essentialism: total	
qui clogit choice pfemale2xfrau $maineffects mathintxdoer carexdoer mathintxthinker carexthinker mathintxcreator carexcreator mathintxhelper carexhelper mathintxpersuader carexpersuader mathintxorganizer carexorganizer apfmeltxfrau apfmpeerxfrau mathintxleist_md												 	[pw=w_t13_std], group(id_ind) or
	matrix E12max = e(b)
qui clogit choice pfemale2xfrau $maineffects 																																																							 intercptxbread hrsworkxflexi sdiscrimxfrau 	[pw=w_t13_std], group(id_ind) or
	matrix E12min = e(b)

* 13 - separate spheres: bread
qui clogit choice pfemale2xfrau $maineffects 																																  																							intercptxbread								 	[pw=w_t13_std], group(id_ind) or
	matrix E13max = e(b)
qui clogit choice pfemale2xfrau $maineffects mathintxdoer carexdoer mathintxthinker carexthinker mathintxcreator carexcreator mathintxhelper carexhelper mathintxpersuader carexpersuader mathintxorganizer carexorganizer apfmeltxfrau apfmpeerxfrau mathintxleist_md 					 hrsworkxflexi sdiscrimxfrau 	[pw=w_t13_std], group(id_ind) or
	matrix E13min = e(b)	
		
* 14 - separate spheres: overwork
qui clogit choice pfemale2xfrau $maineffects 																																  																											hrsworkxflexi				 	[pw=w_t13_std], group(id_ind) or
	matrix E14max = e(b)
qui clogit choice pfemale2xfrau $maineffects mathintxdoer carexdoer mathintxthinker carexthinker mathintxcreator carexcreator mathintxhelper carexhelper mathintxpersuader carexpersuader mathintxorganizer carexorganizer apfmeltxfrau apfmpeerxfrau mathintxleist_md intercptxbread 					sdiscrimxfrau 	[pw=w_t13_std], group(id_ind) or
	matrix E14min = e(b)	

* 15  - separate spheres: total	
qui clogit choice pfemale2xfrau $maineffects 																																  																							intercptxbread	hrsworkxflexi				 	[pw=w_t13_std], group(id_ind) or
	matrix E15max = e(b)
qui clogit choice pfemale2xfrau $maineffects mathintxdoer carexdoer mathintxthinker carexthinker mathintxcreator carexcreator mathintxhelper carexhelper mathintxpersuader carexpersuader mathintxorganizer carexorganizer apfmeltxfrau apfmpeerxfrau mathintxleist_md  							 	sdiscrimxfrau 	[pw=w_t13_std], group(id_ind) or
	matrix E15min = e(b)	

* 16 - discrimination	
qui clogit choice pfemale2xfrau $maineffects 																																  																															sdiscrimxfrau	[pw=w_t13_std], group(id_ind) or
	matrix E16max = e(b)
qui clogit choice pfemale2xfrau $maineffects mathintxdoer carexdoer mathintxthinker carexthinker mathintxcreator carexcreator mathintxhelper carexhelper mathintxpersuader carexpersuader mathintxorganizer carexorganizer apfmeltxfrau apfmpeerxfrau mathintxleist_md intercptxbread hrsworkxflexi 				 	[pw=w_t13_std], group(id_ind) or
	matrix E16min = e(b)

/* results matrix for min max decomposition
	rows: models
	columns:
	1 - rownumber
	2 - baseline coefficient pfemale2xfrau
	3 - pfemale2xfrau when adding var
	4 - max% explained for horizontal segregation
	5 - saturated coefficient pfemale2xfrau
	6 - pfemale2xfrau when droping var
	7 - min %explained for horizontal segregation
*/
matrix decomphor = J($modnr , 7, .)
matrix rownames decomphor = 1_doer 2_thinker 3_creator 4_helper 5_persuader 6_organizer 7_int_total 8_norms_elt 9_norms_peer ///
	10_norms_total 11_mdnoten 12_essent_total 13_bread 14_hrswork 15_sepsphe_total 16_discr
matrix colnames decomphor = 1_rownr 2_base_hori 3_coef_add_hori 4_maxexpl_hori  5_satu_hori  6_coef_drop_hori  7_minexpl_hori
	
*** compute percentage explained (as in Morgan et al 2013)
forvalues i = 1/$modnr {
	matrix decomphor[`i', 1] = `i'
	matrix decomphor[`i', 2] = exp(A[1,1])
	matrix decomphor[`i', 3] = exp(E`i'max[1,1])
	matrix decomphor[`i', 4] = ((decomphor[`i',2]-1)-(decomphor[`i',3]-1))/(decomphor[`i',2]-1)
	matrix decomphor[`i', 5] = exp(B[1,1])
	matrix decomphor[`i', 6] = exp(E`i'min[1,1])
	matrix decomphor[`i', 7] = -1*((decomphor[`i',5]-1)-(decomphor[`i',6]-1))/(decomphor[`i',2]-1)
	}

matrix list decomphor, format(%10.4f)
putexcel set "Output\robustness1_minmaxdecomposition.xlsx", replace
putexcel B2 = matrix(decomphor, names) using "Output\robustness1_minmaxdecomposition.xlsx", replace

*** END OF DOFILE ***
log close
exit, clear
